package com.nsgf.library.ui.page;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.binding_recyclerview.adapter.BaseDataBindingAdapter;
import com.nsgf.library.BR;
import com.nsgf.library.R;
import com.nsgf.library.data.bean.LibraryInfo;
import com.nsgf.library.databinding.ActivitySearchListBinding;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.ui.adapter.HomeSearchListAdapter;
import com.nsgf.library.utils.ToastUtils;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

public class HomeMoreActivity extends BaseActivity {

    StateHolder mState;

    private ActivitySearchListBinding mBinding;

    private int type = 0;//列表类型 0水量 1雨量
    private HomeSearchListAdapter mAdapter;
    private List<LibraryInfo> mDatas = new ArrayList<>();

    private AccountRequester mAccountRequester;

    @Override
    protected void initViewModel() {
        mState = getActivityScopeViewModel(StateHolder.class);
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);

    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        type = getIntent().getIntExtra("type", 0);
        mAdapter = new HomeSearchListAdapter(this, type);
        return new DataBindingConfig(R.layout.activity_search_list, BR.vm, mState)
                .addBindingParam(BR.adapter, mAdapter)
                .addBindingParam(BR.click, new ClickProxy());
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getLifecycle().addObserver(mAccountRequester);
        mBinding = (ActivitySearchListBinding) getBinding();
        mState.titleBar.set(getIntent().getStringExtra("title"));

        mBinding.smartRefreshLayout.autoRefresh();

        mAccountRequester.getRainRecordList().observe(this, listResponseStatus -> {

            mBinding.smartRefreshLayout.finishRefresh();
            if (listResponseStatus.getResponseCode() == 200) {
                mState.patrolFeed.set(listResponseStatus.getData());
            } else {
                ToastUtils.showShortToast(listResponseStatus.getMsg());
            }
            if (mState.patrolFeed.get().size() > 0) {
                mBinding.notdata.setVisibility(View.GONE);
                mBinding.smartRefreshLayout.setVisibility(View.VISIBLE);
            } else {
                mBinding.notdata.setVisibility(View.VISIBLE);
                mBinding.smartRefreshLayout.setVisibility(View.GONE);
            }
        });


        mAccountRequester.getWaterRecordList().observe(this, listResponseStatus -> {

            mBinding.smartRefreshLayout.finishRefresh();
            if (listResponseStatus.getResponseCode() == 200) {
                mState.patrolFeed.set(listResponseStatus.getData());
            } else {
                ToastUtils.showShortToast(listResponseStatus.getMsg());
            }
            if (mState.patrolFeed.get().size() > 0) {
                mBinding.notdata.setVisibility(View.GONE);
                mBinding.smartRefreshLayout.setVisibility(View.VISIBLE);
            } else {
                mBinding.notdata.setVisibility(View.VISIBLE);
                mBinding.smartRefreshLayout.setVisibility(View.GONE);
            }
        });

        mAdapter.setOnItemClickListener(new BaseDataBindingAdapter.OnItemClickListener<LibraryInfo>() {
            @Override
            public void onItemClick(int viewId, LibraryInfo item, int position) {
                if (0 == type) {
                    startActivity(new Intent()
                            .putExtra("projectCode", item.getWaterLevelCode())
                            .putExtra("title", item.getReservoirName())
                            .putExtra("info", item)
                            .setClass(HomeMoreActivity.this, WaterActivity.class));
                } else
                    startActivity(new Intent()
                            .putExtra("title", item.getReservoirName())
                            .putExtra("projectCode", item.getProjectCode())
                            .putExtra("info", item)
                            .setClass(HomeMoreActivity.this, RainDetailActivity.class));
            }
        });

        mBinding.smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getDatas();
            }
        });
    }

    private void getDatas() {

        if (type == 0) {
            mAccountRequester.getWaterMonitoringList(mState.searchContent.get());
        } else {
            mAccountRequester.getRainMonitoringList(mState.searchContent.get());
        }
    }

    public class ClickProxy {

        public void search() {
            mBinding.smartRefreshLayout.autoRefresh();
        }
    }
}
